Documentation Index Fetch the complete documentation index at: https://mintlify.com/jcomte23/Python_vanilla/llms.txt
Use this file to discover all available pages before exploring further.
Lists in Python
In Python, a list is a very versatile data structure used to store an ordered collection of elements. Lists are mutable, meaning you can modify them after creation, and they can contain elements of different types.
Creating Lists
There are several ways to create lists in Python:
# Empty list
lista_vacia = []
# List with numbers
lista_numeros = [ 1 , 2 , 3 , 4 , 5 ]
# Mixed data types
lista_mezclada = [ 1 , "dos" , 3.0 , True ]
Lists can contain elements of different types including numbers, strings, booleans, and even other lists.
Accessing Elements
You can access list elements using indices. Python uses zero-based indexing:
lista = [ "a" , "b" , "c" , "d" , "e" ]
# Access first element
primer_elemento = lista[ 0 ] # "a"
# Access last element
ultimo_elemento = lista[ - 1 ] # "e"
# Slicing - get a sublist
sublista = lista[ 1 : 4 ] # ["b", "c", "d"]
Understanding List Indexing
Positive indices start from 0 (beginning of list)
Negative indices start from -1 (end of list)
lista[start:end] returns elements from start up to (but not including) end
lista[:n] - first n elements
lista[n:] - from index n to end
lista[:] - copy of entire list
Modifying Lists
Change Individual Elements
lista = [ 1 , 2 , 3 , 4 , 5 ]
lista[ 2 ] = 10 # Changes the third element to 10
# Result: [1, 2, 10, 4, 5]
Adding Elements
lista = [ 1 , 2 , 3 ]
# Append - adds element to the end
lista.append( 4 ) # [1, 2, 3, 4]
# Extend - adds multiple elements from another list
lista.extend([ 5 , 6 ]) # [1, 2, 3, 4, 5, 6]
# Insert - adds element at specific position
lista.insert( 2 , 10 ) # Inserts 10 at index 2
append() adds a single element, while extend() adds all elements from an iterable.
Removing Elements
lista = [ 1 , 2 , 3 , 4 , 5 ]
# Remove by value - removes first occurrence
lista.remove( 3 ) # Removes the value 3
# Pop - removes and returns last element (or at specified index)
elemento_eliminado = lista.pop() # Removes last element
# Delete by index
del lista[ 2 ] # Removes element at index 2
Practical Example: Family Members List
Here’s a real-world example managing a family members list:
miembrosDeLaFamilia = [ "tatarabuelo" ]
miembrosDeLaFamiliaExpulsado = []
# Adding members
miembrosDeLaFamilia.append( "abuelo" )
miembrosDeLaFamilia.insert( 1 , "abuela" )
miembrosDeLaFamilia.append( "papa" )
miembrosDeLaFamilia.append( "mama" )
miembrosDeLaFamilia.append( "yo" )
print ( "ANTES:" , miembrosDeLaFamilia)
# Output: ['tatarabuelo', 'abuela', 'abuelo', 'papa', 'mama', 'yo']
# Removing members
del miembrosDeLaFamilia[ 2 ]
miembrosDeLaFamilia.remove( "abuelo" )
miembrosDeLaFamiliaExpulsado.append(miembrosDeLaFamilia.pop( 2 ))
print ( "DESPUES:" , miembrosDeLaFamilia)
print ( "cantidad de miembros:" , len (miembrosDeLaFamilia))
print ( "miembros expulsado:" , miembrosDeLaFamiliaExpulsado)
Sorting Lists
Sort In-Place
The sort() method modifies the original list:
# Sort numbers
numeros = [ 3 , 1 , 4 , 1 , 5 , 9 , 2 ]
numeros.sort()
print (numeros) # [1, 1, 2, 3, 4, 5, 9]
# Sort strings alphabetically
palabras = [ "zebra" , "manzana" , "banana" ]
palabras.sort()
print (palabras) # ['banana', 'manzana', 'zebra']
Sort Without Modifying Original
Use the sorted() function to create a new sorted list:
original = [ 3 , 1 , 4 ]
ordenada = sorted (original)
print (original) # [3, 1, 4] - unchanged
print (ordenada) # [1, 3, 4] - new sorted list
sort() modifies the list in-place and returns None. sorted() returns a new sorted list.
Iterating Over Lists
You can loop through list elements using a for loop:
miembrosDeLaFamilia = [ "papa" , "mama" , "yo" ]
for miembro in miembrosDeLaFamilia:
print ( "#->" , miembro.upper())
# Output:
# #-> PAPA
# #-> MAMA
# #-> YO
Common List Operations
lista = [ 1 , 2 , 3 , 4 , 5 ]
print ( len (lista)) # 5
if "manzana" in frutas:
print ( "Found!" )
lista = [ 1 , 2 , 2 , 3 , 2 , 4 ]
print (lista.count( 2 )) # 3
lista = [ 10 , 20 , 30 , 40 ]
print (lista.index( 30 )) # 2
Converting Between Lists and Tuples
You can convert between lists and tuples:
# Tuple to list
tuplaMesclada = ( "javier" , 234 , "javier" , "hola mundo" )
tuplaMesclada = list (tuplaMesclada)
print (tuplaMesclada) # Now it's a list
# List to tuple
lista = [ 1 , 2 , 3 ]
tupla = tuple (lista)
Key Takeaways
Lists are mutable - you can modify them after creation
Lists are ordered - elements maintain their insertion order
Lists allow duplicates - the same value can appear multiple times
Lists can contain mixed types - numbers, strings, booleans, objects, etc.
Lists support negative indexing - access elements from the end
Use lists when you need a collection that can grow, shrink, or change over time. For fixed collections, consider using tuples instead.